package logic

import (
	"cart_srv/models/redis"
	"context"
	"errors"

	"cart_srv/cart_srv"
	"cart_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SumCartLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSumCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SumCartLogic {
	return &SumCartLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SumCartLogic) SumCart(in *cart_srv.SumCartRequest) (*cart_srv.SumCartResponse, error) {
	// todo: add your logic here and delete this line
	if redis.CartIsNilOrNotNil(int(in.UserId)) {
		return nil, errors.New("购物车为空")
	}
	cart, f := redis.SumCart(int(in.UserId))
	return &cart_srv.SumCartResponse{
		AllNum:   int64(cart),
		AllPrice: float32(f),
	}, nil
}
